﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec
{
    public partial class TimViecLam : System.Web.UI.Page
    {

        public string Tk { set; get; }
        public int NganhNgheId { set; get; }
        public int NoiLamViec { set; get; }
        public int LoadHinhCv { set; get; }
        public int Trang { set; get; }
        protected void Page_Load(object sender, EventArgs e)
        {

            

            if (Request.QueryString["tk"] != null)
            {
                Tk = Request.QueryString["tk"];
            }
            else
            {
                Tk = "";
            }
            if (Request.QueryString["nganhnghe"] != null)
            {
                NganhNgheId = int.Parse(Request.QueryString["nganhnghe"]);
            }
            else
            {
                NganhNgheId = -1;
            }
            if (Request.QueryString["page"] != null)
            {
                Trang = int.Parse(Request.QueryString["page"]);
            }
            else
            {
                Trang = 1;
            }

            if (Request.QueryString["noilam"] != null)
            {
                NoiLamViec = int.Parse(Request.QueryString["noilam"]);
            }
            else
            {
                NoiLamViec = -1;
            }

            if (Request.QueryString["lh"] != null)
            {
                LoadHinhCv = int.Parse(Request.QueryString["lh"]);
            }
            else
            {
                LoadHinhCv = -1;
            }
            if (!IsPostBack)
            {
                LoadData(NganhNgheId, NoiLamViec,LoadHinhCv, Tk);
            }
        }

        private void LoadData(int nganhNgheId, int noiLamViec, int loadHinhCv, string tk)
        {

            ListItem item = new ListItem();
            item.Text = "Tất cả ngành nghế";
            item.Value = "-1";
            item.Selected = true;

            ListItem item1 = new ListItem();
            item1.Text = "Tất cả nơi làm việc";
            item1.Value = "-1";
            item1.Selected = true;

            dropNganhNghe.DataSource = BLL.Admin.NganhNgheHelper.List_NganhNghe_ValueConfig(0);
            dropNganhNghe.DataTextField = "Ten";
            dropNganhNghe.DataValueField = "NganhNgheId";
            dropNganhNghe.DataBind();
            dropNganhNghe.Items.Add(item);

            dropNoiLamViec.DataSource = BLL.Admin.TinhHelper.List_ALL_Tinh();
            dropNoiLamViec.DataTextField = "Ten";
            dropNoiLamViec.DataValueField = "TinhId";
            dropNoiLamViec.DataBind();
            dropNoiLamViec.Items.Add(item1);

            string path = HttpContext.Current.Request.Url.AbsolutePath;
            
            int page = 1;

            if (Request.QueryString["page"] != null)
            {
                page = int.Parse(Request.QueryString["page"]);
            }

            var data = BLL.NguoiDung.TuyenDung.SearchTuyenDung(nganhNgheId, noiLamViec, loadHinhCv, tk);
            rptTinTuyenDung.DataSource = data.Skip((page - 1)*20).Take(20);
            rptTinTuyenDung.DataBind();

            int Count = data.Count();
            int TotalPage = Count / 20+ 1;
            if (Count < 1)
            {
                lbtPreview.Visible = false;
                lbtNext.Visible = false;
            }
            if (page == 1)
            {
                lbtPreview.Visible = false;
            }
            if (page == TotalPage)
            {
                lbtNext.Visible = false;
            }
            for (int i = page - 4; i < page + 4; i++)
            {
                if (i < 1)
                    continue;
                if (i > TotalPage)
                    continue;
                if (i == page && Count > 0)
                    ltlPaging.Text += "<li><a class='current' href='" + path + "?nganhnghe=" + nganhNgheId + "&noilam=" + noiLamViec + "&lh=" + loadHinhCv +"&tk=" + tk + "&page=" + i + "'>" + i + "</a></li>";
                else
                    ltlPaging.Text += "<li><a href='" + path + "?nganhnghe=" + nganhNgheId + "&noilam=" + noiLamViec + "&lh=" + loadHinhCv + "&tk=" + tk + "&page=" + i + "'>" + i + "</a></li>";
            }
        }


        protected void lbtPreview_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?nganhnghe=" + NganhNgheId + "&noilam=" + NoiLamViec + "&lh=" + LoadHinhCv + "&tk=" + Tk + "&page="+(Trang - 1) );
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?nganhnghe=" + NganhNgheId + "&noilam=" + NoiLamViec + "&lh=" + LoadHinhCv + "&tk=" + Tk + "&page=" + (Trang + 1));
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            var str = "";
            if (txtSearch.Text != "")
            {
                str = "&tk=" + txtSearch.Text;
            }
            Response.Redirect("/tim-viec-lam.html?nganhnghe=" + dropNganhNghe.SelectedValue + "&noilam=" + dropNoiLamViec.SelectedValue + "&lh=" + dropLoaiHinhCongViec.SelectedValue + str);
        }
    }
}